﻿package jakey.SalaryCount;

import jakey.SalaryCount.SQLite.dbHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CountSalaryActivity extends Activity {
	private String user_name;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.count);
		Button countBtn = (Button)findViewById(R.id.count_btn);
		countBtn.setOnClickListener(new CountBtnListener());
		
		Button clearBtn = (Button)findViewById(R.id.clear_btn);
		clearBtn.setOnClickListener(new ClearBtnListener());
		Intent intent = getIntent();
		user_name = intent.getStringExtra("user_name");
		TextView name = (TextView)findViewById(R.id.count_name);
		name.setText("  --  " + user_name);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	class CountBtnListener implements OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			EditText et_lv1 = (EditText)findViewById(R.id.lv1);
			String lv1 = et_lv1.getText().toString();
			
			EditText et_lv2 = (EditText)findViewById(R.id.lv2);
			String lv2 = et_lv2.getText().toString();
			
			EditText et_lv3 = (EditText)findViewById(R.id.lv3);
			String lv3 = et_lv3.getText().toString();

			EditText et_lv4 = (EditText)findViewById(R.id.lv4);
			String lv4 = et_lv4.getText().toString();
			int lv1Val = 0,lv2Val = 0,lv3Val = 0,lv4Val = 0;
			try {
				lv1Val = Integer.parseInt(lv1);
				lv2Val = Integer.parseInt(lv2);
				lv3Val = Integer.parseInt(lv3);
				lv4Val = Integer.parseInt(lv4);
			} catch (Exception e) {
				// TODO: handle exception
				showMsg("请填写数字");
				return;
			}
			dbHelper dbHelper = new dbHelper(CountSalaryActivity.this, "salaryCount_db", null);
			Cursor cursor =  dbHelper.select("baseinfo", null, null, null, null, null, null);			
			int v1 = 0,v2 = 0, v3 = 0, v4 = 0;
			while (cursor.moveToNext()) {
				v1 = Integer.parseInt(cursor.getString(cursor.getColumnIndex("lv1")));
				v2 = Integer.parseInt(cursor.getString(cursor.getColumnIndex("lv2")));
				v3 = Integer.parseInt(cursor.getString(cursor.getColumnIndex("lv3")));
				v4 = Integer.parseInt(cursor.getString(cursor.getColumnIndex("lv4")));
			}
			cursor.close();
			cursor = dbHelper.select("user", null, "user_name=?", new String[]{user_name}, null, null, null);
			int salary = 0;
			String major = null;
			while(cursor.moveToNext()){
				salary = Integer.parseInt(cursor.getString(cursor.getColumnIndex("salary")));
				major = cursor.getString(cursor.getColumnIndex("major"));
			}
			cursor.close();
			dbHelper.close();
			int res = lv1Val * v1 + lv2Val * v2 + lv3Val * v3 + lv4Val * v4 + salary;
			String formula = "  (底薪) "+salary+"\n + (小学) " + lv1Val + " * " + v1 + "\n + (初中) " + lv2Val + " * " + v2 + "\n + (高中) " + lv3Val + " * " + v3 + "\n + (高三) " + lv4Val + " * " + v4;			
			Intent intent = new Intent();
			intent.putExtra("res", res);
			intent.putExtra("user_name", user_name);
			intent.putExtra("major", major);
			intent.putExtra("formula", formula);
			intent.setClass(CountSalaryActivity.this, CountResultActivity.class);
			CountSalaryActivity.this.startActivity(intent);
		}		
	}
	
	//清空按钮监听器
	class ClearBtnListener implements OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			EditText et_lv1 = (EditText)findViewById(R.id.lv1);
			et_lv1.setText("");
			EditText et_lv2 = (EditText)findViewById(R.id.lv2);
			et_lv2.setText("");
			EditText et_lv3 = (EditText)findViewById(R.id.lv3);
			et_lv3.setText("");
			EditText et_lv4 = (EditText)findViewById(R.id.lv4);
			et_lv4.setText("");
		}
		
	}
	
	//提示信息
	public void showMsg(String msg) {
		Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
		toast.show();
	}
	
}
